import axios from "axios";
import { reject } from "core-js/fn/promise";
/* 
    ajax请求函数模块
*/
export default function ajax(url,data={},type='GET'){
    return new Promise((resolve,reject)=>{
        let promise
        if(type === 'GET'){
            // 准备 url query 参数数据
            let dataStr=''  //数据拼接字符串
            data.map(item=>{
                dataStr+= (data + '=' + data[item] + '&')
            })
            if(dataStr !== ''){
                // 去除最后面的'&'
                dataStr = dataStr.subString(0,dataStr.lastIndexOf('&'))
                url = url + '?' +dataStr
            }
            // 发送get请求
            promise=axios.get(url)
        }else{
            promise = promise.post(url,data)
        }

        promise.then(response =>{
            resolve(response.data)
        }).catch(error=>{
            reject(error)
        })
    })
}